//
//  JXStatusPictureView.swift
//  01-weiboSwift
//
//  Created by TwistFate on 16/3/3.
//  Copyright © 2016年 TwistFate. All rights reserved.
//

import UIKit


let JXPictureCellMargin : CGFloat = 10

let maxColumn = 3

let pictureCellWH = (UIScreen.width - (CGFloat(maxColumn) - 1)*JXPictureCellMargin - 2*JXStatusCellMargin)/CGFloat(maxColumn)

private let CZStatusPictureViewCellSize = CGSize(width: pictureCellWH, height: pictureCellWH)

private let ReuseIdentifier = "ReuseIdentifier"

class JXStatusPictureView: UICollectionView {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private let layout = UICollectionViewFlowLayout()
    
    init () {
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        
        backgroundColor = UIColor.clearColor()
//         设置数据源和代理
        self.delegate = self
        self.dataSource = self
        
//         注册cell
        self.registerClass(JXStatusPictrueCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
    }
    
    var status : JXStatus?  {
        didSet{
            let size = calculateSize()
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
                
            }
            
//             刷新collectionView
            self.reloadData()
        }
    }
    
    
    
    private func calculateSize () ->CGSize {
        let count = status?.pic_urls?.count ?? 0
        print("\(count)")
       
        if count == 0 {
            return CGSizeZero
        }
        
        if count == 1 {
            let size = CGSize(width: 180, height: 120)
            
            // cell和collectionView一样大
            layout.itemSize = size
            layout.minimumInteritemSpacing = 0
            layout.minimumLineSpacing = 0
            return size
        }
        
        // 设置layout
        layout.itemSize = CZStatusPictureViewCellSize
        layout.minimumLineSpacing = JXPictureCellMargin
        layout.minimumInteritemSpacing = JXPictureCellMargin
        
        // 其它情况
        // 计算列数
        // 判断是否是4张,4张显示2列,其它情况判断个数是否大于最大列数,小于最大列数显示count列, 大于最大列数显示最大列数
        let column = count == 4 ? 2: count < maxColumn ? count : maxColumn
        
        // (count + maxColumn - 1) / maxColumn,算页数,前提 maxColumn必须是Int类型
        // count = 5, 8 + 3 - 1 / 3
        let row =  count == 4 ? 2 : (count + maxColumn - 1) / maxColumn
        
        // 计算宽度
        let width = CGFloat(column) * pictureCellWH + (CGFloat(column) - 1) * JXPictureCellMargin
        
        // 计算高度
        let height = CGFloat(row) * pictureCellWH + (CGFloat(row) - 1) * JXPictureCellMargin
        
        print("count: \(count), column: \(column), row: \(row), width: \(width), height: \(height)")
        
        return CGSize(width: width, height: height)
        
    }
}
//
extension JXStatusPictureView : UICollectionViewDataSource ,UICollectionViewDelegate {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return status?.pic_urls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as! JXStatusPictrueCell
        
        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.brownColor()
        
//        cell.imageURL = status?.pic_urls?[indexPath.item]["thumbnail_pic"]
        
        return cell
    }
    
}

class JXStatusPictrueCell: UICollectionViewCell {
   
//    // MARK: - 属性
//    var imageURL: NSURL? {
//        didSet {
//            // sd来显示图片
//            bkgImageView.sd_setImageWithURL(imageURL)
//        }
//    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    private func prepareUI () {
    
        contentView.addSubview(self.bkgImageView)
    
        self.bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
    }
    
    
    lazy var bkgImageView :UIImageView = UIImageView(image: nil)

}



